﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DbLibrary.DataBase;
using FillupData = DbLibrary.Models.FillupData;


namespace MVC_web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var fillup = new FillupDataRepository().GetAll().OrderByDescending(_ => _.Date).Take(10);
            var cars = new CarRepository().GetAll();
            var periods = new ReportPeriodRepository().GetAll().Where(_ => !_.Test);
            var fillView = new List<CommonFillupView>();

            foreach (var f in fillup)
            {
                string carName = cars.First(_ => _.Id == f.CarId).Name ?? "noname";

                fillView.Add(new CommonFillupView
                {
                    Id = f.Id,
                    Date = f.Date,
                    Car = carName,
                    Kilometers = f.Kilometers,
                    Liters = f.Liters,
                    Price = f.Price
                });
            }

            ViewBag.FillView = fillView;

            return View();
        }

        [HttpGet]
        public ActionResult NewGasoline()
        {
            ViewBag.ReportList = new ReportPeriodRepository().GetAll().ToList();
            ViewBag.CarList = new CarRepository().GetAll().ToList();
            return View();
        }

        [HttpPost]
        public ActionResult NewGasoline(FillupData fillup)
        {
            var carName = new CarRepository().SelectById(fillup.CarId).Name;
            var periodName = new ReportPeriodRepository().SelectById(fillup.ReportPeriodId).Name;

            var commonFillup = new CommonFillupView
            {
                Car = carName,
                Date = fillup.Date,
                Kilometers = fillup.Kilometers,
                Liters = fillup.Liters,
                Price = fillup.Price
            };
            return View("GasolineAdded");
        }
    }
}
